﻿#include "mainscence.h"
#include "ui_mainscence.h"

MainScence::MainScence(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainScence)
{
    ui->setupUi(this);

    initState();
}

MainScence::~MainScence()
{
    delete ui;
}

void MainScence::initState()
{
    this->setWindowTitle("FFmpeg Filter滤镜");
    this->setFixedSize(460, 420);
}

QString MainScence::getSelectFilePath()
{
    static QString lastOpenDir = "E:/MediaFile/Video";
    QString fileName = QFileDialog::getOpenFileName(this, "媒体文件", lastOpenDir, "All Files (*)");
    QFileInfo info(fileName);
    lastOpenDir = info.absolutePath();

    return fileName;
}

void MainScence::on_btnYUVFile_clicked()
{
    QString filePath = getSelectFilePath();
    if(filePath.isEmpty())
        return;

    ui->editYUV->setText(filePath);
}

void MainScence::on_btnStartVideoFilter_clicked()
{
    if(ui->editYUV->text().isEmpty() || ui->editWidth->text().isEmpty() || ui->editHeight->text().isEmpty())
        return;

    QString filePath = ui->editYUV->text();
    int width = ui->editWidth->text().toInt();
    int height = ui->editHeight->text().toInt();

    videoFilter.setVideoParm(width, height, filePath);
    videoFilter.setRunState(true);
}

void MainScence::on_btnPCMFile_clicked()
{
    QString filePath = getSelectFilePath();
    if(filePath.isEmpty())
        return;

    ui->editPCM->setText(filePath);
}

void MainScence::on_btnPCMFile_2_clicked()
{
    QString filePath = getSelectFilePath();
    if(filePath.isEmpty())
        return;

    ui->editPCM_2->setText(filePath);
}

void MainScence::on_btnStartAudioMix_clicked()
{
    if(ui->editPCM->text().isEmpty() || ui->editSampleRate->text().isEmpty() || ui->editChannel->text().isEmpty())
        return;

    if(ui->editPCM_2->text().isEmpty() || ui->editSampleRate_2->text().isEmpty() || ui->editChannel_2->text().isEmpty())
        return;

    AudioParm parm1;
    parm1.sampleRate = ui->editSampleRate->text().toInt();
    parm1.channel = ui->editChannel->text().toInt();
    parm1.byteSize = (ui->cbBoxFormat->currentText() == "S16") ? 2 : 4;
    parm1.format = (ui->cbBoxFormat->currentText() == "S16") ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_FLT;
    parm1.filePath = ui->editPCM->text().toStdString();

    AudioParm parm2;
    parm2.sampleRate = ui->editSampleRate_2->text().toInt();
    parm2.channel = ui->editChannel_2->text().toInt();
    parm2.byteSize = (ui->cbBoxFormat_2->currentText() == "S16") ? 2 : 4;
    parm2.format = (ui->cbBoxFormat_2->currentText() == "S16") ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_FLT;
    parm2.filePath = ui->editPCM_2->text().toStdString();

    audioFilter.setAudioParm(parm1, parm2);
    audioFilter.setRunState(true);
}
